import $state from '@/uni_modules/uni-im/sdk/state/index.js';
import $methods from '@/uni_modules/uni-im/sdk/methods/index.js';
import appEvent from './appEvent';
import createObservable from './createObservable';
import toFriendlyTime from './toFriendlyTime';
import shortcutKey from './shortcut-key.js';
import parseHtml from './html-parser.js';
import markdownIt from './markdown-it.min.js';
import md5 from './md5.min.js'
// 导入i18n
import messages from '../../i18n/index.js'
import {
	initVueI18n
} from '@dcloudio/uni-i18n'
const {
	t: $w
} = initVueI18n(messages)
// 注意，此处在设置多语言翻译时要做特殊设置 __WJF
const localeLang = uni.getLocale()

export default {
	appEvent,
	createObservable,
	toFriendlyTime,
	shortcutKey,
	parseHtml,
	markdownIt,
	/**
	 *深度合并多个对象的方法
	 */
	deepAssign() {
		let len = arguments.length,
			target = arguments[0]
		if (!this.isPlainObject(target)) {
			target = {}
		}
		for (let i = 1; i < len; i++) {
			let source = arguments[i]
			if (this.isPlainObject(source)) {
				for (let s in source) {
					if (s === '__proto__' || target === source[s]) {
						continue
					}
					if (this.isPlainObject(source[s])) {
						target[s] = this.deepAssign(target[s], source[s])
					} else {
						target[s] = source[s]
					}
				}
			}
		}
		return target
	},
	/**
	 * 替换文本中的url为套了html的a标签的方式
	 */
	replaceUrlToLink(str) {
		// 找网址
		let urlPattern = /(https?:\/\/|www\.)[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g;
		return str.replace(urlPattern, function(match) {
			var href = match;
			if (match.indexOf("http") == -1) {
				//如果不带http://开头就带上
				href = "http://" + match;
			}
			return `<a class="link" target="_blank" href="${href}">${match}</a> `;
		});
	},
	/**
	 *判断对象是否是一个纯粹的对象
	 */
	isPlainObject(obj) {
		return typeof obj === 'object' && Object.prototype.toString.call(obj) === '[object Object]'
	},
	getScreenMetrics() {
		let metrics = {
			pageLeft: 0,
			pageTop: 0,
			pageWidth: $state.systemInfo.screenWidth,
			pageHeight: $state.systemInfo.screenHeight,
		};
		[
			'screenWidth',
			'screenHeight',
			'windowWidth',
			'windowHeight',
			'windowTop',
			'windowBottom',
			'statusBarHeight',
			'safeArea',
			'safeAreaInsets',
		].forEach(name => {
			metrics[name] = $state.systemInfo[name]
		})
		try {
			uni.createSelectorQuery()
				.select('uni-page-body > #page, uni-page-body > .page')
				.boundingClientRect(data => {
					if (data) {
						metrics['pageLeft'] = data.left
						metrics['pageTop'] = data.top
						metrics['pageWidth'] = data.width
						metrics['pageHeight'] = data.height
					}
				})
				.exec()
		} catch (e) {
			console.error('调用 uni.createSelectorQuery 时机过早：', e)
		}
		return metrics
	},
	isMuteMsg(msg) {
		return (
			// TODO：静默消息
			msg.is_mute === true ||
			// 加群消息
			msg.action === "join-group" ||
			// 禁言通知
			msg.action === 'update-group-info-mute_all_members'
		)
	},
	isReadableMsg(msg) {
		// 如果是扩展的消息类型，则由扩展程序决定结果
		let result = $methods.msgTypes.get(msg.type)?.isReadable()
		if (typeof result !== 'undefined') return result
		return msg.type !== 'revoke_msg' &&
			msg.action !== 'update-group-info-avatar_file' &&
			msg.type !== 'clear-conversation-unreadCount'
	},
	filterMsgList(msgList) {
		return msgList.filter(msg => this.isReadableMsg(msg))
	},
	getMsgNote(_msg) {
		const msg = JSON.parse(JSON.stringify(_msg))
		const type = msg.type;
		let note = '[' + $w('多媒体') + ']'
		if (msg.is_revoke) {
			note = $w("消息已被撤回")
		} else if (msg.is_delete) {
			note = $w("消息已被删除")
		} else if (type === 'text') {
			note = msg.body.toString()
		} else if (type === 'userinfo-card') {
			// 注意，此处在设置多语言翻译时要做特殊设置  __WJF
			note = {
				"zh-Hans": `[${msg.body.name} 的名片]`,
				en: `[Business Card of ${msg.body.name}]`,
				ru: `[Визитная карточка ${msg.body.name}]`,
				kk: `[${msg.body.name}-дің визиткасы]`
			} [localeLang] || `[Business Card of ${msg.body.name}]`
		} else {
			note = {
				"image": "[" + $w("图片") + "]",
				"sound": "[" + $w("语音") + "]",
				"video": "[" + $w("视频") + "]",
				"file": "[" + $w("文件") + "]",
				"location": "[" + $w("位置") + "]",
				"system": "[" + $w("系统通知") + "]",
				"code": "[" + $w("代码") + "]",
				"rich-text": "[" + $w("富文本消息") + "]",
				"revoke_msg": "[" + $w("消息已被撤回") + "]",
			} [type] || `[${type}]`

			if (type == "system") {
				note = {
					"join-group": "[" + $w("新用户加入群聊") + "]",
					"group-exit": "[" + $w("退出群聊") + "]",
					"group-expel": "[" + $w("被移出群聊") + "]",
					"group-dissolved": "[" + $w("此群已被解散") + "]"
				} [msg.action] || '[' + $w("系统消息") + ']'
			} else if (type == "rich-text") {
				note = getRichTextText(msg.body)

				function getRichTextText(nodesList) {
					return getTextNode(nodesList)

					function getTextNode(nodesList) {
						let text = '';
						nodesList.forEach(item => {
							if (item.type == "text") {
								text += item.text.replace(/(\r\n|\n|\r)/gm, "")
							} else {
								text += ({
									'image': '['+$w('图片')+']',
									'link': '['+$w('链接')+']',
								})[item.type] || ''
							}
							if (item.name === 'img') {
								text += ' ['+$w('图片')+'] '
							}
							if (item.attrs && item.attrs.class === 'nickname' && item.attrs.user_id) {
								let userInfo = $state.users[item.attrs.user_id]
								delete item.children
								if (userInfo) {
									text += ` @${userInfo.nickname} `
								} else {
									text += '@['+$w('昵称加载中')+'...]'
									$state.users.get(item.attrs.user_id)
								}
							}
							if (Array.isArray(item.children)) {
								text += getTextNode(item.children)
							}
						})
						// 替换换行为空格
						return text || '';
					}
				}
			}
		}
		// 去掉换行符
		note = note.replace(/(\r\n|\n|\r)/gm, "");
		// 改 &nbsp; &emsp; &ensp;等空格为普通空格
		note = note.replace(/&nbsp;|&emsp;|&ensp;/g, ' ')
		// 截取80个字符
		note = note.slice(0, 80).trim()
		return note
	},
	// 节流执行函数，用于控制频繁触发的事件。
	throttle(fn, delay) {
		fn.timer && clearTimeout(fn.timer);
		fn.timer = setTimeout(fn, delay);
	},
	async sleep(time) {
		return await new Promise((resolve, reject) => {
			setTimeout(() => {
				resolve()
			}, time)
		})
	},
	getConversationId(id, type = 'single') { //single,group
		if (type == 'single') {
			let current_uid = uniCloud.getCurrentUserInfo().uid
			if (!current_uid) {
				console.error('错误current_uid不能为空', current_uid);
			}
			let param = [id, current_uid]
			return 'single_' + md5(param.sort().toString())
		} else {
			return 'group_' + id
		}
	},
	async getTempFileURL(fileid) {
		// console.log('getTempFileURL', fileid)
		// 如果不是fileid就直接返回。
		if (!fileid || fileid.indexOf('blob:') === 0 || fileid.indexOf('data:image/png;base64,') === 0 || fileid
			.substring(0, 8) != "cloud://") {
			return fileid
		}
		let res = await uniCloud.getTempFileURL({
			fileList: [fileid]
		})
		return res.fileList[0].tempFileURL
	}
}